﻿using OpenMms.Common.Cathe;
using OpenMms.Common.Session;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenMms.Common.Auth
{
    /// <summary>
    /// 用户缓存信息操作
    /// </summary>
    public class LocalAuth:IAuth
    {
        #region 初始化数据
        private IHttpContextAccessor _httpContextAccessor;
        private ICacheContext _cacheContext;

        public LocalAuth(IHttpContextAccessor httpContextAccessor
            , ICacheContext cacheContext)
        {
            _httpContextAccessor = httpContextAccessor;
            _cacheContext = cacheContext;
        }
        #endregion

        #region 获取Token
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        private string GetToken()
        {
            //_httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

            string token = _httpContextAccessor.HttpContext.Request.Query["Token"];
            if (!String.IsNullOrEmpty(token)) return token;

            token = _httpContextAccessor.HttpContext.Request.Headers["X-Token"];
            if (!String.IsNullOrEmpty(token)) return token;

            var cookie = _httpContextAccessor.HttpContext.Request.Cookies["Token"];
            return cookie ?? String.Empty;
        }
        #endregion

        #region 判断是否登录
        /// <summary>
        /// 判断是否登录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="otherInfo"></param>
        /// <returns></returns>
        public bool CheckLogin(string token = "", string otherInfo = "")
        {
            if (string.IsNullOrEmpty(token))
            {
                token = GetToken();
            }

            if (string.IsNullOrEmpty(token))
            {
                return false;
            }

            try
            {
                var result = _cacheContext.Get<UserAuthSession>(token) != null;
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取当前登录的用户信息
        /// <summary>
        /// 获取当前登录的用户信息
        /// <para>通过URL中的Token参数或Cookie中的Token</para>
        /// </summary>
        /// <param name="otherInfo">The otherInfo.</param>
        /// <returns>LoginUserVM.</returns>
        public UserAuthSession GetCurrentUser()
        {
           return _cacheContext.Get<UserAuthSession>(GetToken());
        }
        #endregion

        #region 获取当前登录的用户名
        /// <summary>
        /// 获取当前登录的用户名
        /// <para>通过URL中的Token参数或Cookie中的Token</para>
        /// </summary>
        /// <param name="otherInfo">The otherInfo.</param>
        /// <returns>System.String.</returns>
        public string GetUserName()
        {
            var user = _cacheContext.Get<UserAuthSession>(GetToken());
            if (user != null)
            {
                return user.Account;
            }

            return "";
        }
        #endregion

        #region 注销
        /// <summary>
        /// 注销
        /// </summary>
        public bool Logout()
        {
            var token = GetToken();
            if (String.IsNullOrEmpty(token)) return true;

            try
            {
                _cacheContext.Remove(token);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 获取IP
        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public virtual string GetIp()
        {
            return  _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
        }
        #endregion
    }
}
